package com.fishery.customization.controller;

import com.fishery.customization.entity.DraggableModule;
import com.fishery.customization.entity.vo.UpdateDraggableModuleCheckedVo;
import com.fishery.customization.service.DraggableModuleCheckedService;
import com.fishery.customization.service.DraggableModuleService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customization/drag")
@Api(tags = "拖拽模块管理")
@CrossOrigin
public class DraggableModuleController {

	@Autowired
	private DraggableModuleService draggableModuleService;

	@Autowired
	private DraggableModuleCheckedService draggableModuleCheckedService;

	@GetMapping("/all")
	@ApiOperation("查询所有可拖拽的模块")
	public Result searchAllDraggableModules() {
		return Result.success().data(draggableModuleService.getAllDraggableModules());
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除可拖拽模块")
	public Result deleteDraggableModule(@PathVariable(name = "id")
										@ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		return draggableModuleService.removeDraggableModuleById(id)
				? Result.success("删除成功") : Result.error("删除失败");
	}

	@DeleteMapping("/batch")
	@ApiOperation("批量删除可拖拽模块")
	public Result deleteDraggableModuleWithBatch(@RequestBody @ApiParam(value = "ids") List<String> ids) {
		if (ids == null || ids.size() == 0) {
			return Result.error("批量删除失败");
		}
		return draggableModuleService.removeDraggableModuleWithBatch(ids)
				? Result.success("批量删除成功") : Result.error("批量删除失败");
	}

	@PutMapping
	@ApiOperation("修改可拖拽模块")
	public Result updateDraggableModule(@RequestBody DraggableModule draggableModule) {
		if (draggableModule == null || StringUtils.isEmpty(draggableModule.getId())) {
			return Result.error("修改失败");
		}
		return draggableModuleService.updateDraggableModuleById(draggableModule)
				? Result.success("修改成功") : Result.error("修改失败");
	}

	@PostMapping
	@ApiOperation("添加可拖拽模块")
	public Result addDraggableModule(@RequestBody DraggableModule draggableModule) {
		return draggableModuleService.saveDraggableModule(draggableModule)
				? Result.success("添加成功") : Result.error("添加失败");
	}

	@PostMapping("/batch")
	@ApiOperation("批量添加可拖拽模块")
	public Result addDraggableModuleWithBatch(@RequestBody List<DraggableModule> modules) {
		return draggableModuleService.saveDraggableModule(modules)
				? Result.success("添加成功") : Result.error("添加失败");
	}

	@GetMapping("/checked/{userId}")
	@ApiOperation("查询用户已经选中模块的顺序")
	public Result searchDraggableModuleCheckedOfUser(@PathVariable(name = "userId")
													 @ApiParam(value = "用户Id", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data(draggableModuleCheckedService.getDraggableModuleCheckedByUserId(userId));
	}

	@PutMapping("/checked")
	@ApiOperation("保存或更新用户已经选中模块的顺序")
	public Result insertOrUpdateDraggableModuleCheckedOfUser(@RequestBody @ApiParam(value = "updateDraggableModuleCheckedVo")
																	 UpdateDraggableModuleCheckedVo vo) {
		if (vo == null
				|| vo.getModuleChecked() == null
				|| StringUtils.isEmpty(vo.getUserId())) {
			return Result.error("操作失败");
		}
		return draggableModuleCheckedService.saveOrUpdateDraggableModuleCheckedByUserId(vo)
				? Result.success("操作成功") : Result.error("操作失败");
	}

}
